package com.pk.platform.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateConverter {

	/**
     * yyyy-MM-dd
     */
	public static String FORMAT_ONE = "yyyy-MM-dd"; //格式一

	/**
     * yyyy/MM/dd
     */
	public static String FORMAT_TWO = "yyyy/MM/dd"; //格式二
	
	/**
     * yyyy-MM-dd HH:mm:ss
     */
	public static String FORMAT_THREE = "yyyy-MM-dd HH:mm:ss"; //格式三
	
	/**
     * yyyy/MM/dd HH/mm/ss
     */
	public static String FORMAT_FOUR = "yyyy/MM/dd HH/mm/ss"; //格式四

	/**
	 * yyyyMMddHHmmss
	 */
	public static String FORMAT_FIVE = "yyyyMMddHHmmss"; //格式五

	public static String FORMAT_SIX = "yyyy-MM-dd HH:mm"; //格式六

	public static String FORMAT_SEVEN = "yyyyMMdd"; //格式七
	
	public static String SUM = "sum";

	public static String SUB = "sub";
	
    /**
     * <p>
     * 说明:Date转String
     * @param date 日期
     * @param format 格式
     * @return 
     * </p>
     */
    public static String dateToString(Date date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = "";
        if(!"".equals(date) && date != null){
            s = sdf.format(date);
        }
       
        return s;
    }
    
    /**
     * <p>
     * 说明:String转Date
     * @param date 日期
     * @param format 格式
     * @return 
     * </p>
     */
    public static Date StringToDate(String s, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            if(!"".equals(s) && s != null){
                date = sdf.parse(s);
            }
          
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    
    /**
     * <p>
     * 说明:获得当前系统的年
     * </p>
     * @return
     */
    public static int getCurrentYear()
    {
    	Calendar calendar = Calendar.getInstance();
    	int year = calendar.get(Calendar.YEAR);
    	return year;
    }
    
    
    /**
     * <p>
     * 说明:获得当前系统的月
     * </p>
     * @return
     */
    public static int getCurrentMonth()
    {
    	Calendar calendar = Calendar.getInstance();
    	int month = calendar.get(Calendar.MONTH) + 1;
    	return month;
    }
    
    
    /**
     * 获得当前年字串####
     * @return
     */
    public static String getCurrentYearStr()
    {
    	String today = getToday();
    	return String.valueOf(getYearByDate(today));
    }
    
    
    /**
     * 获得当前月字串##
     * @return
     */
    public static String getCurrentMonthStr()
    {
    	String today = getToday();
    	return String.valueOf(getMonthByDate(today));
    }
    
    
    /**
     * 获得当前日字串##
     * @return
     */
    public static String getCurrentDayStr()
    {
    	String today = getToday();
    	return String.valueOf(getDayByDate(today));
    }
    
    
    /**
     * 今天日期
     * @return
     */
    public static String getToday()
    {
    	Date date = new Date();
    	return dateToString(date, FORMAT_ONE);
    }
    
    
    /**
     * 根据日期得到年（日期格式为：yyyy-MM-dd）
     * @param date
     * @return
     */
    public static int getYearByDate(String date)
    {
    	return Integer.parseInt(date.substring(0, 4));
    }
    
    
    /**
     * 根据日期得到月（日期格式为：yyyy-MM-dd）
     * @param date
     * @return
     */
    public static int getMonthByDate(String date)
    {
    	return Integer.parseInt(date.substring(5, 7));
    }

    /**
     * 根据日期得到年（返回字串，日期格式为：yyyy-MM-dd）
     * @param date
     * @return
     */
    public static String getYearStrByDate(String date)
    {
    	return date.substring(0, 4);
    }
    
    
    /**
     * 根据日期得到月（返回字串，日期格式为：yyyy-MM-dd）
     * @param date
     * @return
     */
    public static String getMonthStrByDate(String date)
    {
    	return date.substring(5, 7);
    }
    
    
    /**
     * 根据日期得到日（日期格式为：yyyy-MM-dd）
     * @param date
     * @return
     */
    public static int getDayByDate(String date)
    {
    	return Integer.parseInt(date.substring(8, 10));
    }
    
    
    /**
	 * <p>
	 * 说明:得到最后几年
	 * </p>
	 * @param count 最后N年
	 * @return
	 */
	public static List<SelectOption> getLastNYear(int count)
	{
		List<SelectOption> list = new ArrayList<SelectOption>();
		int thisYear = getCurrentYear();
		int lastYear = thisYear - count;
		for(int i = thisYear; i > lastYear; i--)
		{
			SelectOption so = new SelectOption(i + "", i + "");
			list.add(so);
		}
		return list;
	}
	
	
	/**
	 * <p>
	 * 说明:得到前后几年
	 * </p>
	 * @param count 前后N年
	 * @return
	 */
	public static List<SelectOption> getBeforeAfterNYear(int count)
	{
		List<SelectOption> list = new ArrayList<SelectOption>();
		int thisYear = getCurrentYear();
		int beforeYear = thisYear - count;
		int afterYear = thisYear + count;
		for(int i = beforeYear; i <= afterYear; i++)
		{
			SelectOption so = new SelectOption(i + "", i + "");
			list.add(so);
		}
		return list;
	}

	
	/**
	 * <p>
	 * 说明:得到所有月份
	 * </p>
	 * @return
	 */
	public static List<SelectOption> getAllMonth(boolean isNeedZero)
	{
		List<SelectOption> list = new ArrayList<SelectOption>();
		for(int i = 1; i <= 12; i++)
		{
			String v = i + "";
			if(i < 10 && isNeedZero)
				v = "0" + v;
			SelectOption so = new SelectOption(v, v);
			list.add(so);
		}
		return list;
	}

	/**
	 * <p>
	 * 说明:得到所有月份(label显示时加什么)
	 * </p>
	 * @return
	 */
	public static List<SelectOption> getAllMonthWidthChn(boolean isNeedZero)
	{
		List<SelectOption> list = new ArrayList<SelectOption>();
		for(int i = 1; i <= 12; i++)
		{
			String v = i + "";
			if(i < 10 && isNeedZero)
				v = "0" + v;
			SelectOption so = new SelectOption(v, v + "月");
			list.add(so);
		}
		return list;
	}
	
	public static String floatToDateString(Double number){
	    String begin = "1899-12-30 00:00:00";
        Date d = DateConverter.StringToDate(begin, DateConverter.FORMAT_THREE);
        Long beginTime = d.getTime();
        Double dou = (number * 3600) * 24 * 1000;
        Long sum = beginTime + dou.longValue();
        Date date = new Date(sum);
        String result = DateConverter.dateToString(date, DateConverter.FORMAT_THREE);
	    return result;
	}
	
	public static Date dateCompute(Date date, int day, String sign)
	{
		Long dd = date.getTime();
		Long d = new Long(day * 24 * 60 * 60 * 1000);
		Long result = 0L;
		if(SUM.equals(sign))
		{
			result = dd + d;
		}
		else if(SUB.equals(sign))
		{
			result = dd - d;
		}
		Date newDate = new Date(result);
		return newDate;
	}

	public static int dayLast(Date date1, Date date2)
	{
		Long d1 = date1.getTime();
		Long d2 = date2.getTime();
		Long sub = 0L;
		if(d1 > d2)
		{
			sub = d1 - d2;
		}
		else if(d1 < d2)
		{
			sub = d2 - d1;
		}
		int result = (int)(sub / ( 24 * 60 * 60 * 1000));
		return result;
	}
	
}
